/*
 * PlaceModel.js defines simple handler for 
 * place data JSON received from Qype API
 * 
 * PlaceModel functions return single data items
 * parsed from JSON for snippets and views
 * generating UI
 */

/*
 * This is a sample of single place data JSON received from Qype API
 * 
 * {"place":{"opening_hours":"Monday, Sunday: closed, Tuesday - Wednesday: 19:00 - 1:00, Thursday - Saturday: 20:00 - 3:00","updated":"2010-08-05T16:55:15+02:00","point":"53.5532,9.95799","closed":false,"image":{"medium":"http://assets3.qype.com/uploads/photos/0175/2419/-365847936_thumb.jpg","medium2x":"http://assets2.qype.com/uploads/photos/0175/2419/-365847936_large.jpg","large":"http://assets3.qype.com/uploads/photos/0175/2419/-365847936_gallery.jpg","small":"http://assets2.qype.com/uploads/photos/0175/2419/-365847936_mini.jpg"},
 * "address":{"country_code":"DE","postcode":"22767","city":"Hamburg","housenumber":"2-4","street":"Am Brunnenhof"},"title":"Luba Luft","categories":[{"full_title":{"value":"Cocktail Bars","lang":"en"},"updated":"2009-12-09T10:21:15+01:00","title":{"value":"Cocktail Bars","lang":"en"},"links":[{"href":"http://api.qype.com/v1/place_categories/249","rel":"self"},{"href":"http://api.qype.com/v1/place_categories/22","rel":"http://schemas.qype.com/place_category.parent"}],"id":"tag:api.qype.com,2007-11-02:/places/categories/249",
 * "created":"2007-11-02T15:35:46+01:00"},{"full_title":{"value":"Clubs","lang":"en"},"updated":"2009-12-09T10:21:38+01:00","title":{"value":"Clubs","lang":"en"},"links":[{"href":"http://api.qype.com/v1/place_categories/23","rel":"self"},{"href":"http://api.qype.com/v1/place_categories/23/children","rel":"http://schemas.qype.com/place_categories.children"},{"href":"http://api.qype.com/v1/place_categories/2","rel":"http://schemas.qype.com/place_category.parent"}],"id":"tag:api.qype.com,2007-08-23:/places/categories/23",
 * "created":"2007-08-23T14:16:38+02:00"}],"links":[{"href":"http://api.qype.com/v1/places/67370","rel":"self"},{"href":"http://www.qype.co.uk/place/67370-Luba-Luft-Hamburg","rel":"alternate","hreflang":"en"},{"href":"http://api.qype.com/v1/places/67370/reviews/de","count":59,"rel":"http://schemas.qype.com/reviews","hreflang":"de"},{"href":"http://api.qype.com/v1/places/67370/reviews/en","count":22,"rel":"http://schemas.qype.com/reviews","hreflang":"en"},{"href":"http://api.qype.com/v1/places/67370/reviews/fr",
 * "count":2,"rel":"http://schemas.qype.com/reviews","hreflang":"fr"},{"href":"http://api.qype.com/v1/places/67370/reviews/es","count":1,"rel":"http://schemas.qype.com/reviews","hreflang":"es"},{"href":"http://api.qype.com/v1/places/67370/reviews/pt","count":0,"rel":"http://schemas.qype.com/reviews","hreflang":"pt"},{"href":"http://api.qype.com/v1/places/67370/reviews/pl","count":1,"rel":"http://schemas.qype.com/reviews","hreflang":"pl"},{"href":"http://api.qype.com/v1/places/67370/reviews/it",
 * "count":1,"rel":"http://schemas.qype.com/reviews","hreflang":"it"},{"href":"http://api.qype.com/v1/locators/de600-hamburg-st-pauli","rel":"http://schemas.qype.com/locator","title":"St. Pauli"},{"href":"http://api.qype.com/v1/places/67370/tags","rel":"http://schemas.qype.com/tags","hreflang":null},{"href":"http://api.qype.com/v1/places/67370/assets","count":36,"rel":"http://schemas.qype.com/assets"},{"href":"http://api.qype.com/v1/positions/53.5532,9.95799/places?without=67370","rel":"http://schemas.qype.com/places.nearby"},
 * {"href":"http://api.qype.com/v1/places/67370/attributes","rel":"http://schemas.qype.com/attributes"},{"href":"http://api.qype.com/v1/places/67370/checkins","count":13,"rel":"http://schemas.qype.com/checkins"},{"href":"http://api.qype.com/v1/places/67370/leaderboard","rel":"http://schemas.qype.com/checkins_rankings"},{"href":"http://api.qype.com/v1/likes?likeable_id=67370\u0026likeable_type=place","count":10,"rel":"http://schemas.qype.com/likes"}],"url":"http://www.Lubaluft.de","phone":"+49 176 48194332",
 * "id":"tag:api.qype.com,2007-10-09:/places/67370","created":"2007-10-09T16:18:39+02:00","average_rating":5}};
 */



qype.models.PlaceModel = Class.create({

	// Initialize model data
	initialize: function(data, lang) {
		
	  /* ***************************
	   PLACE OF CODE SNIPPET 12.1 
	  ****************************/
		
        if(!data) {
            throw "Initializing PlaceModel without data not allowed!";
            return;
        }
        
        this.contentLang = lang;
        this.data = data;

        this.reviews = undefined;
        this.categoriesStr = undefined;
	},

	// Return number of reviews for the place
    getReviewsCount: function() {
        if(this.data.links && !this.reviews) {
            for(var i = 0; i < this.data.links.length; i++) { // Loop through JSON (see the sample data)
                var link = this.data.links[i];

                if(link.rel == "http://schemas.qype.com/reviews" && link.hreflang == this.contentLang) {
                    this.reviews = link;
                    break;
                }
            }
        }

        if(this.reviews) {
            return this.reviews.count || 0;
        }
        else {
            return 0;
        }
    },

    getReviewsStr: function() {
        var count = this.getReviewsCount();
        return count + ' ' + ((count == 1) ? qype.str.place_reviews_count_single_postfix : qype.str.place_reviews_count_postfix);
    },

    getDistanceStr: function() {
        return (this.data.distance ?
                    (Math.round(this.data.distance * 100) / 100) : "-") + " " + qype.str.unit_km;
    },

    getId: function() {
        return this.data.id.substring(this.data.id.lastIndexOf("/") + 1);
    },

    getCategoriesStr: function() {
        if(this.data.categories && !this.categoriesStr) {
            var arr = [];
            for(var i = 0; i < this.data.categories.length; i++) {
                arr.push(this.data.categories[i].title.value);
            }
            this.categoriesStr = arr.join(", ");
        }

        return this.categoriesStr || "";
    },

	// Return image url of the given size
    getImageUrl: function(size) {
        if(this.data.image) {
            return this.data.image[size];
        }

        return "";
    }
});